package cn.kgc.controller;

import cn.kgc.dto.Dto;
import cn.kgc.dto.DtoUtil;
import cn.kgc.utils.BaseException;
import cn.kgc.utils.MD5;
import cn.kgc.vo.TokenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.service.UserService;

import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/user/api/")
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping("/p/login")
    public Dto login(@RequestParam String phone, @RequestParam String password) throws Exception {
        //入参进行操作
        String secrtPassword= MD5.getMd5(password,32);
        //实现登录的操作
        Object[] objects=   userService.queryLogin(phone,secrtPassword);
        return DtoUtil.returnDataSuccess(objects);
    }


    @RequestMapping("/v/replaceToken")
    public Dto replaceToken(@RequestHeader String token) throws BaseException {
      TokenVO tokenVO= userService.replaceToken(token);
      return DtoUtil.returnDataSuccess(tokenVO);
    }


    /**
     * 这个接口只需要发送短信，报错，自定异常直接返回
     * @param phone
     * @return
     */
    @RequestMapping("/p/logincode")
    public Dto sendPhoneCode(@RequestParam String phone) throws BaseException, ExecutionException, InterruptedException {
          userService.sendLoginCode(phone);
          return DtoUtil.returnSuccess();
    }


    @RequestMapping("p/phonelogin")
    public Dto validPhoneCode(@RequestParam  String phone, @RequestParam String vcode){
        Object[] objects=  userService.checkPhoneLoginCode(phone,vcode);
         return DtoUtil.returnDataSuccess(objects);
    }






}
